package main

import (
	"fmt"
	"reflect"
)

func reflectTest(b interface{})  {

	//获取到reflect.Type
	rTyp := reflect.TypeOf(b)
	fmt.Printf("rTyp=%T\n",rTyp)
	fmt.Println("rTyp=",rTyp)

	//获取到reflect.Value
	rVal := reflect.ValueOf(b)
	fmt.Printf("rVal=%T\n",rVal)
	fmt.Println("rVal=",rVal)

	//将rVal转化成interface{},再转成基本数据类型
	iV := rVal.Interface()
	num2 := iV.(int)
	fmt.Println("num2=",num2)

}

func reflectTest02(b interface{})  {

	//获取到reflect.Type
	rTyp := reflect.TypeOf(b)
	fmt.Printf("rTyp=%T\n",rTyp)
	fmt.Println("rTyp=",rTyp)

	//获取到reflect.Value
	rVal := reflect.ValueOf(b)
	fmt.Printf("rVal=%T\n",rVal)
	fmt.Println("rVal=",rVal)

	//将rVal转化成interface{},再转成基本数据类型
	iV := rVal.Interface()
	if num2,ok := iV.(Student);ok{
		fmt.Println("num2=",num2)
		fmt.Printf("num2.Name = %s\n",num2.name)
	}

	kind1 := rTyp.Kind()
	kind2 := rVal.Kind()
	fmt.Printf("类别rType=%v,rVal=%v",kind1,kind2)
	

}

type Student struct {
	name string
	age int
}

func main()  {
	//基本数据类型、interface{}、reflect.Value进行相互转化

	var num int = 100
	reflectTest(num)

	s := Student {
		name : "tom",
		age : 20,
	}

	reflectTest02(s)
}
